<template>
  <div class="app-container">
    <el-card class="query-card" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
        <el-form-item label="登录系统" prop="loginSystem">
          <el-select v-model="queryParams.loginSystem" placeholder="请选择登录系统" clearable>
            <el-option
              v-for="dict in dict.type.sys_login_system"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="登录地址" prop="ipaddr">
          <el-input
            v-model="queryParams.ipaddr"
            placeholder="请输入登录地址"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleForceLogoutBatch"
            v-hasPermi="['monitor:online:remove']"
          >强退</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <vxe-table
        ref="tableRef"
        :loading="loading"
        resizable
        :row-config="{isHover: true}"
        :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)"
        @checkbox-all="handleSelectionChange"
        @checkbox-change="handleSelectionChange"
        :class="showSearch ? 'table-container' : 'table-container-full'"
      >
        <vxe-column type="checkbox" align="center" min-width="60"/>
        <vxe-column title="序号" type="seq" align="center" min-width="80">
          <template slot-scope="scope">
            <span>{{(pageNum - 1) * pageSize + scope.$rowIndex + 1}}</span>
          </template>
        </vxe-column>
        <vxe-column title="会话编号" align="center" field="tokenId" min-width="180" :show-overflow="true" />
        <vxe-column title="登录系统" align="center" field="loginSystem" min-width="150">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_login_system" :value="scope.row.loginSystem"/>
          </template>
        </vxe-column>
        <vxe-column title="登录名称" align="center" field="userName" min-width="180" :show-overflow="true" />
        <vxe-column title="主机" align="center" field="ipaddr" min-width="180" :show-overflow="true" />
        <vxe-column title="登录时间" align="center" field="loginTime" min-width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.loginTime) }}</span>
          </template>
        </vxe-column>
        <vxe-column title="操作" align="center" fixed="right" min-width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleForceLogout(scope.row)"
              v-hasPermi="['monitor:online:forceLogout']"
            >强退</el-button>
          </template>
        </vxe-column>
      </vxe-table>
      <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />
    </el-card>
  </div>
</template>

<script>
import { list, forceLogout } from "@/api/monitor/online";

export default {
  name: "Online",
  dicts: ['sys_login_system'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      selectRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        ipaddr: undefined,
        userName: undefined,
        loginSystem: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(row) {
      this.selectRows = [...row.records];
      this.single = row.records.length !== 1;
      this.multiple = !row.records.length;
    },
    /** 强退按钮操作 */
    handleForceLogout(row) {
      this.$modal.confirm('是否确认强退用户名为"' + row.userName + '"的用户？').then(function() {
        return forceLogout(row.tokenId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("强退成功");
      }).catch(() => {});
    },
    /** 批量强退 */
    async handleForceLogoutBatch() {
      const that = this;
      const userNames = this.selectRows.map(item => item.userName);
      let logoutUserName = [];
      this.$modal.confirm('是否确认强选择的用户名为["' + (userNames.join('、') || '无选择用户') + '"]的用户？').then(async () => {
        for (let row of that.selectRows) {
          await forceLogout(row.tokenId).then(res => {
            if (res.code === 200) {
              logoutUserName.push(row.userName);
            }
          });
        }
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('强退用户[' + (logoutUserName.join('、') || '无选择用户') + ']成功');
      });
    }
  }
};
</script>
<style lang="scss" scoped>

</style>

